import { Dialog } from "vant";

const modelFunctions = {};

let serviceList = [
  "测试服务1",
  "测试服务2",
  "测试服务3",
  "测试服务4",
  "测试服务5",
  "测试服务6"
];

modelFunctions.urlParam = function(url) {
  url = url == null ? window.location.href : url;
  let search = url.substring(url.lastIndexOf("?") + 1);
  let obj = {};
  let reg = /([^?&=]+)=([^?&=]*)/g;
  search.replace(reg, function(rs, $1, $2) {
    let name = decodeURIComponent($1);
    let val = decodeURIComponent($2);
    val = String(val);
    obj[name] = val;
    return rs;
  });
  return obj;
};

modelFunctions.setMenus = function(data) {
  let tempArr = [],
    newArr = [];
  for (let i = 0; i < data.length; i++) {
    if (tempArr.indexOf(data[i].sceneId) === -1) {
      newArr.push({
        serviceName: data[i].sceneId,
        icon: require("@/assets/img/home_center_menu_5.png"),
        bottomMenus: [data[i]]
      });
      tempArr.push(data[i].sceneId);
    } else {
      for (let j = 0; j < newArr.length; j++) {
        if (newArr[j].serviceName === data[i].sceneId) {
          newArr[j].bottomMenus.push(data[i]);
        }
      }
    }
  }
  for (let i = 0; i < newArr.length; i++) {
    newArr[i].serviceName = serviceList[i];
  }
  return newArr;
};

modelFunctions.sortCards = function(arr) {
  // arr.forEach(item => {
  //   item.sort = JSON.stringify(item.seq);
  // });
  if (arr.length <= 1) {
    return arr;
  } else {
    let same = [];
    let inner = [];
    for (let i = 0; i < arr.length; i++) {
      for (let j = 1; j < arr.length; j++) {
        if (arr[i].seq === arr[j].seq && i !== j) {
          let validate = {
            isHas: false,
            index: null
          };
          same.forEach((item, index) => {
            if (item.seq === arr[i].seq) {
              validate.isHas = true;
              validate.index = index;
            }
          });
          if (validate.isHas) {
            let sameItem = same[validate.index];
            sameItem.index = [...new Set([...sameItem.index, i, j])];
          } else {
            same.push({ seq: arr[i].seq, index: [i, j] });
          }
        }
      }
    }

    for (let x = 0; x < arr.length; x++) {
      for (let y = 0; y < same.length; y++) {
        let obj = {
          seq: arr[x].seq,
          sub: []
        };
        if (arr[x].seq !== same[y].seq) {
          obj.sub.push(arr[x]);
          inner.push(obj);
        }
      }
    }
    let sameInner = [];
    for (let i = 0; i < same.length; i++) {
      let obj = {
        seq: same[i].seq,
        sub: []
      };
      for (let j = 0; j < same[i].index.length; j++) {
        obj.sub.push(arr[same[i].index[j]]);
      }
      sameInner.push(obj);
    }

    let arry = inner.concat(sameInner);
    for (let j = 0; j < arry.length - 1; j++) {
      for (let i = 0; i < arry.length - 1 - j; i++) {
        if (arry[i].seq > arry[i + 1].seq) {
          let temp = arry[i];
          arry[i] = arry[i + 1];
          arry[i + 1] = temp;
        }
      }
    }
    let last = [];
    for (let y = 0; y < arry.length; y++) {
      for (let j = 0; j < arry[y].sub.length; j++) {
        last.push(arry[y].sub[j]);
      }
    }
    return last;
  }
};

modelFunctions.sortNumber = function(arry) {
  for (let j = 0; j < arry.length - 1; j++) {
    for (let i = 0; i < arry.length - 1 - j; i++) {
      if (arry[i].seq > arry[i + 1].seq) {
        let temp = arry[i];
        arry[i] = arry[i + 1];
        arry[i + 1] = temp;
      }
    }
  }
  let last = [];
  for (let y = 0; y < arry.length; y++) {
    for (let j = 0; j < arry[y].sub.length; j++) {
      last.push(arry[y].sub[j]);
    }
  }
  return last;
};

modelFunctions.checkJumpType = function(data) {
  // data = decodeURIComponent(data);
  let back = {
    type: "",
    params: {}
  };
  if (data) {
    let d = data.indexOf("http");
    let s = data.indexOf("https");
    // let w = data.indexOf("ymt://wechate/applet");
    let w = data.indexOf("ymt:");
    let u = data.indexOf("/auth/oauth/authorize");
    if (w !== -1) {
      back.type = "wx";
      if (data.indexOf("redirect_uri") !== -1) {
        // let str = data.split("redirect_uri=");
        console.log("module", data);
        back.url = "1";
        back.params = data;
      } else {
        back.params = this.urlParam(data);
      }
      // back.params = this.urlParam(data);
      return back;
    } else if (d === 0 || s === 0) {
      if (u !== -1) {
        back.type = "h5";
      } else {
        back.type = "h5_anonymous";
      }
      return back;
    }
  }
  // return back;
};

// modelFunctions.transCards = function(arry) {
//   let newArry = new Array(arry.length);
//   for (let i = 0; i < arry.length; i++) {
//     let num = arry[i].seq;
//     newArry[num] = arry[i];
//   }
//   return newArry;
// };

modelFunctions.transVouchers = function(arry1, arry2) {
  let newArry = [];
  // https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/
  // http://101.75.247.246:8088/citizen/rest/app/api/v1/image/
  for (let i = 0; i < arry2.length; i++) {
    let obj = {};
    for (let j = 0; j < arry1.length; j++) {
      if (arry2[i].cardTypeId === arry1[j].cardTypeId) {
        obj.cardTypeId = arry1[j].cardTypeId;
        obj.icon =
          arry1[j].icon.indexOf("http") !== -1
            ? arry1[j].icon
            : "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
              arry1[j].icon;
        obj.background = arry1[j].background;
        obj.seq = arry1[j].seq;
        obj.title = arry1[j].title;
        obj.pageUrl = arry1[j].pageUrl;
        obj.available = arry1[j].available;
        obj.expireDate = arry2[i].expireDate;
        newArry.push(obj);
      }
    }
  }
  return newArry;
};

modelFunctions.transCards = function(arry1, arry2) {
  // https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/
  for (let i = 0; i < arry1.length; i++) {
    arry1[i].icon =
      arry1[i].icon.indexOf("http") !== -1
        ? arry1[i].icon
        : "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
          arry1[i].icon;
    for (let j = 0; j < arry2.length; j++) {
      if (arry2[j].cardTypeId === arry1[i].cardTypeId) {
        arry1[i].state = arry2[j].state;
      }
    }
  }
  return arry1;
};

modelFunctions.transBanner = function(arry) {
  // https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/
  for (let i = 0; i < arry.length; i++) {
    arry[i].img =
      arry[i].img.indexOf("http") !== -1
        ? arry[i].img
        : "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
          arry[i].img;
  }
  return arry;
};

modelFunctions.transMenus = function(arry1, arry2) {
  // https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/
  for (let i = 0; i < arry1.length; i++) {
    arry1[i].icon =
      arry1[i].icon.indexOf("http") !== -1
        ? arry1[i].icon
        : "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
          arry1[i].icon;
    arry1[i].iconSelected =
      arry1[i].iconSelected.indexOf("http") !== -1
        ? arry1[i].iconSelected
        : "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
          arry1[i].iconSelected;
    arry1[i].bottomMenus = [];
    for (let j = 0; j < arry2.data.length; j++) {
      if (arry2.data[j].sceneId === arry1[i].id) {
        arry2.data[j].icon =
          arry2.data[j].icon.indexOf("http") !== -1
            ? arry2.data[j].icon
            : "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
              arry2.data[j].icon;
        arry1[i].bottomMenus.push(arry2.data[j]);
      }
    }
  }
  return arry1;
};

modelFunctions.transIcon = function(data) {
  for (let i = 0; i < data.length; i++) {
    if (data[i].icon) {
      if (data[i].icon.indexOf("http") === -1) {
        data[i].icon =
          "https://service.neuqsoft.com/ggfw/citizen/rest/app/api/v1/image/" +
          data[i].icon;
      }
    }
  }
  return data;
};

modelFunctions.httpTip = function(data) {
  if (!data.ok) {
    Dialog.alert({
      message: data.message
    });
    return false;
  }
  return true;
};

modelFunctions.checkDate = function() {
  let current = new Date().getTime();
  let sessionDate = localStorage.getItem("expire_in");
  return current - sessionDate <= 2 * 60 * 60 * 1000;
};

export default modelFunctions;
